package com.example.demo.controller;

import com.example.demo.entity.QuestionsOptions;
import com.example.demo.entity.TestQuestions;
import com.example.demo.service.QuestionsService;
import com.example.demo.utils.R;
import com.example.demo.utils.ReSet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class QuestionsController {

    @Resource
    private QuestionsService questionsService;

    //查询章节试题
    @PostMapping(value = "selsect")
    public R selsect(Integer chapter_id){
        List<TestQuestions> list = questionsService.selsectlist(chapter_id);
        List<TestQuestions> testQuestionslist = new ArrayList<>();
        if (list != null){
            for(TestQuestions t:list){
                TestQuestions testQuestions = questionsService.selsect(t.getTestQuestionsId());
                List<QuestionsOptions> questionsOptions = questionsService.selopts(t.getTestQuestionsId());
                testQuestions.setChilden(questionsOptions);
                testQuestionslist.add(testQuestions);
            }
            return ReSet.Ok(testQuestionslist);

        }

        return ReSet.Err();
    }

    /**
     * 查询试卷题目
     * */
    @PostMapping("seltpque")
    public R seltpque(Integer cid) {

        List list3 = new ArrayList();
        if (questionsService.seltpque(cid).equals("数据为空")){
            return ReSet.Err("查询失败，数据不存在");
        }else{
            String list1 = questionsService.seltpque(cid);
//            int x = questionsService.seltpqueid(cid);
//            List<Integer> list2 = questionsService.selpptid(x);
            List<Integer> list2 = questionsService.selpptid(cid);
            int j = list2.size();
            List list4 = new ArrayList();
            for(int k = 0;k < j;k++){
                int l = (int)list2.get(k);
                List list5 = new ArrayList();
                String names = questionsService.seltqname(l);
                List<QuestionsOptions> list = questionsService.selqops(l);
                list5.add(names);
                list5.add(list);
                list4.add(list5);
            }
            list3.add(list1);
//            list3.add(x);
            list3.add(list4);

            return ReSet.Ok(list3);
        }
    }

    //查询试题
    @GetMapping(value = "selquestion")
    public R selquestion(Integer page,Integer limit) {

        List<TestQuestions> list = questionsService.selquestion(page,limit);
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }
    @GetMapping(value = "selquestion1")
    public R selquestion1() {

        List<TestQuestions> list = questionsService.selquestion1();
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }
    @GetMapping(value = "selquenums")
    public R selquenums() {
        int i = questionsService.selquenums();
        return ReSet.Ok(i);
    }

    //查询答案
    @PostMapping(value = "selqueopt")
    public R selqueopt(Integer test_questions_id) {
        List<QuestionsOptions> list = questionsService.selqueopt(test_questions_id);
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }

    /**
     * 模糊查询
     */
    @PostMapping("selmuhu")
    public R selmuhu(String test_questions_name,Integer test_questions_picture,Integer test_questions_history) {
        List<TestQuestions> list = questionsService.selmuhu(test_questions_name, test_questions_picture, test_questions_history);
        return ReSet.Ok(list);
    }

    //查询试题ID
    @PostMapping("selque")
    public R selque(String name) {
        int i = questionsService.selque(name);
        if(i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    //增加试题
    @PostMapping(value = "insertquestion")
    public R insertquestion(String test_questions_name, Integer test_questions_difficulty, Integer test_questions_language, Integer test_questions_picture, Integer test_questions_history, Integer test_questions_score) {
        int i = questionsService.insertquestion(test_questions_name,test_questions_difficulty,test_questions_language,test_questions_picture,test_questions_history,test_questions_score);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    //增加试题答案
    @PostMapping(value = "inserqueopt")
    public R inserqueopt(Integer test_questions_id, String questions_options_content, Integer questions_options_type) {
        int i = questionsService.inserqueopt(test_questions_id,questions_options_content,questions_options_type);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    //增加章节试题
    @PostMapping(value = "insertpaperques")
    public R insertpaperques(Integer test_paper_id, Integer test_questions_id) {
        int i = questionsService.insertpaperques(test_paper_id,test_questions_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    //增加试卷试题
    @PostMapping("inspques")
    public R inspques(Integer cid, Integer tqid) {
            int j = questionsService.selspques(cid, tqid);
            if (j > 0){
                return ReSet.Err("试题已存在");
            }else {
                int i = questionsService.inspques(cid, tqid);
                if (i > 0){
                    return ReSet.Ok(i);
                }else {
                    return ReSet.Err("添加失败");
                }
            }

    }

    //更改试题
    @PostMapping(value = "updatetestques")
    public R updatetestques(String test_questions_name, Integer test_questions_difficulty, Integer test_questions_picture, Integer test_questions_history, Integer test_questions_score, Integer test_questions_id) {
        int i = questionsService.updatetestques(test_questions_name,test_questions_difficulty,test_questions_picture,test_questions_history,test_questions_score,test_questions_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    //删除试题
    @PostMapping(value = "deleteques")
    public R deleteques(Integer test_questions_id) {
            questionsService.delqueopt(test_questions_id);//删除答案
            int y = questionsService.selcques(test_questions_id);
            if (y > 0){
                questionsService.delcques(test_questions_id);
            }
            int m = questionsService.selpicques(test_questions_id);
            if (m > 0){
                questionsService.delpicques(test_questions_id);
            }
            int i = questionsService.deleteques(test_questions_id);//删除试题
            if( i > 0){
                return ReSet.Ok("删除成功");
            }else {
                return ReSet.Err("删除失败");
            }
    }

    //删除章节试题
    @PostMapping(value = "delsecques")
    public R delsecques(Integer chapter_id, Integer paper_question_id) {
        int i =  questionsService.delsecques(chapter_id,paper_question_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    //上传图片
    @PostMapping("intpic")
    public R intpic(MultipartFile file,Integer test_questions_id){
        R r = questionsService.upload(file,test_questions_id);
        if (r != null){
            return ReSet.Ok(r);
        }

        return ReSet.Err();
    }

    /**
     * 查询是否正确答案
     * */
    @PostMapping("seloptty")
    public R seloptty(Integer questions_options_id) {
        int i = questionsService.seloptty(questions_options_id);
        return ReSet.Ok(i);
    }

    /**
     * 更改试题选项
     */
    @PostMapping("upoption")
    public R upoption(String qocent,Integer qotype, Integer qoid,Integer tqid) {
        int j = questionsService.upoptionall(tqid);
        if (j > 0){
            int i = questionsService.upoption(qocent,qotype, qoid,tqid);
            if (i > 0){
                return ReSet.Ok("更改成功");
            }
        }
        return ReSet.Err("更改失败");

    }

    /**
     * 增加章节试题
     */
    @PostMapping("inscqion")
    public R inscqion(Integer cid,Integer qid){
        int i = questionsService.selcqion(cid, qid);
        if (i > 0){
            return ReSet.Err("试题已存在");
        }else {
            int j = questionsService.inscqion(cid, qid);
            if (j > 0){
                return ReSet.Ok("试题添加成功");
            }else {
                return ReSet.Err("试题添加失败");
            }
        }
    }

    /**
     * 查询章节试题
     */
    @PostMapping("selcqnms")
    public R selcqnms(Integer cid){
        List<Integer> list = questionsService.selcqus(cid);
        int i = list.size();
        List list1 = new ArrayList();
        System.out.println(list);
        for(int j = 0;j < i;j++){
            List<TestQuestions> nm = questionsService.seltqnm(list.get(j));
            list1.add(nm);
        }
        return ReSet.Ok(list1);
    }

    /**
     * 查询试题图片
     * @param tqid
     * @return
     */
    @PostMapping("selqucpic")
    public R selqucpic(Integer tqid) {
        List pic = questionsService.selqucpic(tqid);
        if (pic != null && pic.size() > 0){
            return ReSet.Ok(pic);
        }else {
            return ReSet.Err();
        }
    }
}
